package com.ftg.elvis.service;

import com.ftg.elvis.Util.Constent;
import com.ftg.elvis.pojo.Apply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class AdminMailService {
	@Autowired
	private JavaMailSender javaMailSender;

	public String sendMail(Apply apply) throws Exception{
		/*
		MimeMessage mimeMessage=javaMailSender.createMimeMessage();
		//multipart模式
		try {
			MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage, true);
			mimeMessageHelper.setTo(apply.getAemail());//收件人邮箱user.getMail()
			mimeMessageHelper.setFrom(Constent.MY_EMAIL);//发件人邮箱
			mimeMessage.setSubject(Constent.SUBJECT);//主题

			//设置邮件文本
			if ("审核中".equals(apply.getAstate())) {
				mimeMessageHelper.setText(Constent.AUDIT_TEXT, false);
			}else if("审核通过".equals(apply.getAstate())){
				mimeMessageHelper.setText(Constent.SUCCESS_TEXT, false);
			}else if("未通过".equals(apply.getAstate())){
				mimeMessageHelper.setText(Constent.FAIL_TEXT, false);
			}

			javaMailSender.send(mimeMessage);

			return "SUCCESSFUL";
		} catch (MessagingException e) {
			e.printStackTrace();
			return "FAILFUL";
		}*/

		//建立邮件消息
		SimpleMailMessage mainMessage = new SimpleMailMessage();
		//发送者
		mainMessage.setFrom(Constent.MY_EMAIL);
		//接收者

		mainMessage.setTo(apply.getAemail());

		//发送的标题
		mainMessage.setSubject(Constent.SUBJECT);
		//发送的内容
		if ("审核中".equals(apply.getAstate())) {
			mainMessage.setText(Constent.AUDIT_TEXT);
		}else if("审核通过".equals(apply.getAstate())){
			mainMessage.setText(Constent.SUCCESS_TEXT);
		}else if("未通过".equals(apply.getAstate())){
			mainMessage.setText(Constent.FAIL_TEXT);
		}

		javaMailSender.send(mainMessage);

		return "发送成功";
	}

}
